package com.cos.shop.web.servlet;

import com.cos.shop.common.Const;
import com.cos.shop.common.ResponseCode;
import com.cos.shop.common.ResponseResult;
import com.cos.shop.entity.User;
import com.cos.shop.service.UserServices;
import com.cos.shop.service.impl.UserServicesImpl;
import com.cos.shop.util.Base64Utils;
import com.cos.shop.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "UserController",value = "/user")
public class UserController extends BaseServlet {
    private UserServices us = new UserServicesImpl();

    public String login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        ResponseResult rr = us.doLogin(username,password);

        if(rr.getCode()== ResponseCode.SUCCESS.getCode()){
            request.getSession().setAttribute(Const.SystemConst.IS_LOGIN,rr.getData());

            String auto = request.getParameter("auto");


            if(StringUtils.isNotEmpty(auto)){

                Cookie cookie = new Cookie(Const.SystemConst.AUTO_LOGIN, Base64Utils.encode(username+":"+password));

                cookie.setMaxAge(60*60*24*14);
                cookie.setPath("/");
                response.addCookie(cookie);
            }

            return Const.REDIRECT+"/index.jsp";
        }else{
            request.setAttribute("rr",rr);
            return Const.FORWARD+"/login.jsp";
        }
        
    }


    public String register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String gender = request.getParameter("gender");

        User u = new User(username,password,email,gender);

        boolean flag =us.register(u);

        if (flag){
            return Const.REDIRECT+"/registerSuccess.jsp";
        }else {
            request.getSession().setAttribute("registerMsg","注册失败,请稍后再试");
            return Const.REDIRECT+"/register.jsp";
        }
    }

    public String checkUserName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");

        User user = us.checkUserName(username);

        if (user!=null) {
            return Const.SUCCESS;
        }else{
            return Const.FAIL;
        }
    }

    public String activate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String e = request.getParameter("e");
        String c = request.getParameter("c");

        ResponseResult rr = us.doActivate(e,c);

        request.setAttribute("rr",rr);

        return Const.FORWARD+"/message.jsp";
    }
}
